#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 20-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

using Thn;
using Thn.Drawing;
using Thn.Drawing.Painters;
#endregion

namespace Thn.Interface.Vcl.Themes.BlueSea
{
    /// <summary>
    /// Custom renderer for <see cref="TabControl"/>.
    /// </summary>
#if DEBUG
    public
#else
    internal
#endif
 class TabControlRenderer : TabControlRendererBase
    {
        #region Fields
        Random ran = new Random();
        #endregion

        #region Properties
        #endregion

        #region On Render Tabs Region Visual
        /// <summary>
        /// Paint the background of tabs region.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        protected override void OnRenderTabsRegionVisual(TabControlBase control, Graphics canvas, TabControlRenderContext context)
        {
            
        }
        #endregion

        #region On Render Tab Visual
        /// <summary>
        /// Paint the background of a specific tab.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        /// <param name="tab">Information about the tab to be painted.</param>
        protected override void OnRenderTabVisual(TabControlBase control, Graphics canvas, TabControlRenderContext context, TabRenderContext tab)
        {
            Rectangle r;
            PainterBase painter = null;

            //render background
            r = tab.Bounds;
            r = new Rectangle(r.Left, r.Top, r.Width, r.Height - 0);
            if (tab.IsSelected)
            {
                painter = new White3D();
            }
            else
            {
                painter = new DarkGrayFlat();
            }
            GraphicsState state = canvas.Save();
            canvas.SetClip(new Rectangle(r.Left, r.Top, r.Width + 1, r.Height));
            GraphicsPath path = GraphicUtils.MakeRoundedRectangle(r, true, true, false, false, 8);
            path = GraphicUtils.MakeTopRoundedTab(r, 4, false);
            painter.PaintPath(canvas, path, true, true);
            canvas.Restore(state);

            //clean up
            path.Dispose();
        }
        #endregion

        #region On Render Tab Content
        /// <summary>
        /// Paint the content of a specific tab.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        /// <param name="tab">Information about the tab to be painted.</param>
        protected override void OnRenderTabContent(TabControlBase control, Graphics canvas, TabControlRenderContext context, TabRenderContext tab)
        {
            ITheme theme = control.EffectiveTheme;
            Rectangle r;

            #region Render text
            string txt = tab.Container.Text;
            r = tab.TextRegion;
            if ((txt != null) && (txt != ""))
            {
                #region Has text to render

                //lookup necessary font style
                IFontStyle font = null;
                font = theme.FindFont(StyleNames.TabControlNormal);

                //render the text
                if (font != null)
                {
                    font.DrawString(txt, canvas, r, ContentAlignment.MiddleCenter, false);
                }
                #region Defensive tracing
                else
                {
                    //font is not assigned, forget about drawing
                }
                #endregion

                #endregion
            }
            #region Defensive tracing
            else
            {
                //don't draw anything
            }
            #endregion

            #endregion

            //debug
            //canvas.DrawRectangle(Pens.Blue, r);
        }
        #endregion

        #region On Render Tab Close Button
        /// <summary>
        /// Paint the close button of a specific tab.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        /// <param name="tab">Information about the tab to be painted.</param>
        protected override void OnRenderTabCloseButton(TabControlBase control, Graphics canvas, TabControlRenderContext context, TabRenderContext tab)
        {
            //canvas.FillRectangle(Brushes.Red, tab.CloseButtonRegion);
        }
        #endregion

        #region On Render Visual
        /// <summary>
        /// Paint the control's visual stuff (border, background, etc.) onto canvas.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        protected override void OnRenderVisual(IControl control, Graphics canvas, RenderContext context)
        {
            TabControlRenderContext rc = (TabControlRenderContext)context;
            TabControlBase con = (TabControlBase)control;
            Rectangle r = rc.PageRegion;
            int x = 0;
            int y = 0;

            PainterBase painter = null;
            painter = new White3D();
            painter.PaintRectangle(canvas, r, false, true);

            base.OnRenderVisual(control, canvas, context);            

            #region Render border of tabs region
            Pen p = new Pen(Color.FromArgb(207, 207, 207));
            
            //left
            x = r.Left;
            canvas.DrawLine(p, x, r.Top, x, r.Bottom);

            //right
            x = r.Right - 1;
            canvas.DrawLine(p, x, r.Top, x, r.Bottom);

            //bottom
            y = r.Bottom - 1;
            canvas.DrawLine(p, r.Left, y, r.Right, y);

            p.Dispose();
            #endregion
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public TabControlRenderer()
        {
            mDrawVisualCues = false;
        }
        #endregion
    }
}
